package com.example.boxuegu;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.tv.TvContentRating;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class user_Info extends AppCompatActivity implements View.OnClickListener {

    private String spUserName;

    private static final int CHANGE_NICKNAME=1;//修改昵称的自定义常量
    private static final int CHANGE_SIGNATURE=2;//修改签名的自定义常量

    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout title_bar;
    private RelativeLayout rl_nickName;
    private RelativeLayout rl_sex;
    private RelativeLayout rl_signature;
    private TextView tv_nikeName;
    private TextView tv_user_name;
    private TextView tv_sex;
    private TextView tv_signature;
    private String new_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__info);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        spUserName =AnalysisUtils.readLoginUserName(this);//从sharedPreferences中获取登陆时的用户名
        init();
        initData();
        setListener();

    }

    /*
    * 获取回传数据时候使用的跳转方法，第一个参数to表示需要跳转到的界面，
    * 第二个参数requCode表示一个请求码，第三个参数B表示跳转时传递的数据
    * */
    public void enterActivityForResult(Class<?> to,int requestCode,Bundle b){
        Intent intent = new Intent(this,to);
        intent.putExtras(b);
        startActivityForResult(intent,requestCode);
    }
    /*
    * 设置控件的点击事件*/
    private void setListener() {
        tv_back.setOnClickListener(this);
        rl_nickName.setOnClickListener(this);
        rl_sex.setOnClickListener(this);
        rl_signature.setOnClickListener(this);
    }

    /*
    * 获取数据
    * */
    private void initData() {
        UserBean bean=null;
        bean=DBUtils.getInstance(this).getUserInfo(spUserName);
        //首先判断一下数据库是否有数据
        if(bean==null){
            bean=new UserBean();
            bean.userName=spUserName;
            bean.nikeName="问答精灵";
            bean.sex="男";
            bean.signatur="好好学习，天天向上";
            //保存用户信息到数据库
            DBUtils.getInstance(this).saveUserInfo(bean);

        }setValue(bean);

    }
    /*
    * 为界面控件设置值*/
    private void setValue(UserBean bean) {
        tv_nikeName.setText(bean.nikeName);
        tv_user_name.setText(bean.userName);
        tv_sex.setText(bean.sex);
        tv_signature.setText(bean.signatur);
    }

    /*
    * 初始化控件*/
    private void init() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("个人资料");
        title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        rl_nickName = (RelativeLayout) findViewById(R.id.rl_nickName);
        rl_sex = (RelativeLayout) findViewById(R.id.rl_sex);
        rl_signature = (RelativeLayout) findViewById(R.id.rl_signature);
        tv_nikeName = (TextView) findViewById(R.id.tv_nikeName);
        tv_user_name = (TextView) findViewById(R.id.user_name);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_signature = (TextView) findViewById(R.id.tv_signature);

    }
    /*
    * 控件的点击事件*/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                this.finish();
                break;
            case R.id.rl_nickName://昵称点击事件
                String name = tv_nikeName.getText().toString();
                Bundle bdName = new Bundle();
                bdName.putString("content",name);
                bdName.putString("title","昵称");
                bdName.putInt("flag",1);//flag传递1时表示是修改昵称
                enterActivityForResult(chage_user.class,CHANGE_NICKNAME,bdName);
            case R.id.rl_sex:
                String sex=tv_sex.getText().toString();
                sexDialong(sex);
                break;
            case R.id.rl_signature:
                String signture = tv_signature.getText().toString();
                Bundle bdsignture = new Bundle();
                bdsignture.putString("content",signture);
                bdsignture.putString("title","签名");
                bdsignture.putInt("flag",2);
                enterActivityForResult(chage_user.class,CHANGE_SIGNATURE,bdsignture);
                break;
            default:
                break;
        }

    }

    /*
    * 设置性别的弹出框
    * */
    private void sexDialong(String sex) {
        int sexFlag=0;
        if("男".equals(sex)){
            sexFlag=0;
        }else if("女".equals(sex)){
            sexFlag=1;

        }
        final String items[]={"男","女"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("性别");//设置标题
        builder.setSingleChoiceItems(items, sexFlag, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(user_Info.this,items[which],Toast.LENGTH_SHORT).show();
                setSex(items[which]);
            }
        });

    }
    /*
    * 更新界面上的性别数据*/

    private void setSex(String item) {
        tv_sex.setText(item);
        //更新数据库上的性别字段
        DBUtils.getInstance(user_Info.this).updateUserInfo("sex",item,spUserName);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHANGE_NICKNAME:
                if (data != null) {
                    new_info = data.getStringExtra("nickName");
                    if (new_info.isEmpty()) {
                        return;
                    }
                    tv_nikeName.setText(new_info);
                    DBUtils.getInstance(user_Info.this).updateUserInfo("nickName", new_info, spUserName);

                }
                break;
            case CHANGE_SIGNATURE:
                if (data != null) {
                    new_info = data.getStringExtra("signature");
                    if (new_info.isEmpty()) {
                        return;
                    }
                    tv_signature.setText(new_info);
                    DBUtils.getInstance(user_Info.this).updateUserInfo("signature", new_info, spUserName);
                }
                break;
        }
    }
}